<template>
    <BaseDialog width="600px" title="机器人自动回复群消息时间配置" :handleCancel="handleCancel" :dialogVisible="props.dialogVisible">
        <BaseForm labelWidth="180px" type="rule" :handleOk="handleOk" :handleCancel="handleCancel" :items="formItems"
            v-model:form="ruleForm" :rules="rules">
            <el-row>
                <el-col :span="15">
                    <el-form-item prop="replyBeginTime" label="自动回复时间范围">
                        <el-time-picker value-format="HH:mm" format="HH:mm" v-model="ruleForm.replyBeginTime"
                            placeholder="选择开始时间">
                        </el-time-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="2">
                    <div style="line-height:32px;text-align: center;">至</div>
                </el-col>
                <el-col :span="7">
                    <el-form-item prop="replyEndTime" label-width="0px">
                        <el-time-picker value-format="HH:mm" format="HH:mm" v-model="ruleForm.replyEndTime"
                            placeholder="选择结束时间">
                        </el-time-picker>
                    </el-form-item>
                </el-col>
            </el-row>


            <template #desc>
                <div style="color:#E6A23C;margin-left: 30px;margin-bottom: 20px;">注：以上配置只对机器人在微信群触发关键词的自动回复场景生效</div>
            </template>
        </BaseForm>
    </BaseDialog>
</template>
  
<script setup lang="ts">
import { basicConfigService } from '@/api/basicConfig'
const props = defineProps({
    dialogVisible: {
        type: Boolean,
    },
    item: {
        type: Object,
    },
})
const emit = defineEmits(['update:dialogVisible'])

const formItems = ref([
    /*  {
         name: '自动回复时间范围',
         key: 'replayTime',
         type: 'timerange',
         placeholder: '请选择时间范围',
         maxlength: '500',
     }, */
    {
        name: '相同关键词自动回复频率',
        key: 'keyWordMinute',
        type: 'number',
        suffix: '分钟/次',
        min: 1,
        max: 9999,
        precision: 0
    },
])
const rules = reactive({
    replayTime: [{ required: true, message: '请选择', trigger: 'change' }],
    replyBeginTime: [{ required: true, message: '请选择', trigger: 'change' }],
    replyEndTime: [{ required: true, message: '请选择', trigger: 'change' }],
    keyWordMinute: [{ required: true, message: '请输入', trigger: 'change' }],
})
const ruleForm = ref({
    id: '',
    replyBeginTime: '',
    replyEndTime: '',
    keyWordMinute: '',
    replayTime: [],

})
const handleOk = () => {
    let begin = ruleForm.value.replyBeginTime.length == 5 ? ruleForm.value.replyBeginTime + ':00' : ruleForm.value.replyBeginTime;
    let end = ruleForm.value.replyEndTime.length == 5 ? ruleForm.value.replyEndTime + ':00' : ruleForm.value.replyEndTime
    if (begin == end) {
        ElMessage({
            message: '开始时间不能等于结束时间',
            type: 'error',
        })
        return Promise.reject("开始时间不能等于结束时间")
    }

    return basicConfigService.updateKeyWordConfig({
        data: {
            dataId: ruleForm.value.id,
            replyBeginTime: begin,
            replyEndTime: end,
            keyWordMinute: ruleForm.value.keyWordMinute
        },
    })
}
const getKeyWordConfig = () => {
    basicConfigService.getKeyWordConfig({ data: {} }).then((res) => {
        ruleForm.value.id = res.data.id
        ruleForm.value.replyBeginTime = res.data.replyBeginTime
        ruleForm.value.replyEndTime = res.data.replyEndTime
        ruleForm.value.keyWordMinute = res.data.keyWordMinute
        ruleForm.value.replayTime = [res.data.replyBeginTime || '', res.data.replyEndTime || '']
    })
}
const handleCancel = () => {
    emit('update:dialogVisible', false)
}
onMounted(() => {
    getKeyWordConfig()
})
</script>
  
<style lang="scss" scoped></style>
  